from PyQt5.QtGui import QFont
from PyQt5.QtWidgets import QApplication, QMainWindow

import sys
import re
from PyQt5.uic import loadUi
import os

# https://blog.csdn.net/muzihuaner/article/details/106205930?utm_medium=distribute.pc_relevant_t0.none-task-blog-BlogCommendFromMachineLearnPai2-1.nonecase&depth_1-utm_source=distribute.pc_relevant_t0.none-task-blog-BlogCommendFromMachineLearnPai2-1.nonecase

# touch .pylintrc
# vi .pylintrc
# cat .pylintrc
# extension-pkg-whitelist=PyQt5

# ls -a

def self_path():
    # https://www.cnblogs.com/yuandonghua/p/10234842.html
    os.chdir(sys.path[0])
    dir_name = os.path.abspath(os.path.join(os.getcwd(), "."))
    
    return dir_name


def txt_read_to_list():
    # https://www.cnblogs.com/hainabaichuan/p/11830769.html
    filename = "self_words.txt"
    absolute_file_name = self_path() + "\\" + filename
    # 读取整个文件
    # gbk' codec can't decode byte 0xaa in position 2: illegal multibyte sequence
    # https://www.cnblogs.com/chuxin-sweet/p/9401824.html
    with open(absolute_file_name) as file_obj:
        contents = file_obj.read()

        list_self_words = contents.split(",")
        list_self_words.remove("")

    return list_self_words


class MainWindow(QMainWindow):
    def __init__(self, lst_self_words: list):
        #  https://blog.csdn.net/josephzhang1012/article/details/86696107
        QMainWindow.__init__(self)
        self.setFont(QFont('microsoft yahei'))
        # self.setWindowTitle('hide private words')
        self.lst_self_words = lst_self_words
        loadUi("ui/private.ui", self)

        self.btn_clear.clicked.connect(self.clear)
        self.btn_copy.clicked.connect(self.copy)
        self.btn_replace.clicked.connect(self.replace_private)
        self.btn_add_self_words.clicked.connect(self.add_self_words)
        self.btn_save.clicked.connect(self.save)
        self.btn_clear_msg.clicked.connect(self.clear_msg)

    def clear_msg(self):
        self.txt_msg.setText('')

    def clear(self):

        self.msg('clear')
        self.txt_input.setText('')

    def copy(self):
        cb = QApplication.clipboard()
        # cb.setText(self.txt_output.text())
        # https://blog.csdn.net/jia666666/article/details/81511435
        cb.setText(self.txt_output.toPlainText())

        self.msg('copy')

    def replace_private(self):

        self.msg('replace')
        to_replace = self.txt_input.toPlainText()
        for word in self.lst_self_words:
            to_replace = to_replace.replace(word, '***')
        self.txt_output.setText(to_replace)

    def add_self_words(self):

        self.msg('add')
        words = self.txt_add_self_words.toPlainText()
        words = words.strip()
        print(words)
        lst_words = re.split('[,，]', words)
        for word in lst_words:
            if word not in self.lst_self_words:
                self.lst_self_words.append(word)

        self.msg('add done')

    def save(self):

        self.msg('save')
        file = open('self_words.txt', 'w')
        for word in self.lst_self_words:
            file.write(word + ',')

        self.msg('save done')

    def closeEvent(self, e):
        self.save()

    def msg(self, msg):
        self.txt_msg.append(msg)
        print(msg)

# https://www.cnblogs.com/wangtaobiu/p/10821744.html
# os.listdir()

      
def create_file():
    if 'self_words.txt' not in os.listdir(self_path()):
        print('not have file, now create')
        file=open('self_words.txt','w')
        file.close()



def build():
    app = QApplication(sys.argv)

    self_words = txt_read_to_list()
    window = MainWindow(self_words)
    window.show()
    sys.exit(app.exec_())

create_file()
build()
